<template>
    <div class="login-wrap">
        <el-form class="login-container"
                 :model="user" :rules="rules" ref="user">
            <!-- 头部信息-->
            <h3 class="title">用户登录</h3>
            <!-- 输入框input-->
            <el-form-item prop="email">
                <el-input v-model="user.email" type="text" placeholder="账号"/>
            </el-form-item>
            <el-form-item prop="password">
                <el-input v-model="user.password" type="password"
                          placeholder="密码"
                />
            </el-form-item>
            <el-form-item>
                <el-button type="primary" style="width: 100%"
                           :loading="logining"
                           @click="submitForm('user')"
                >
                    登录
                </el-button>

            </el-form-item>

        </el-form>


    </div>
</template>

<script>
import axios from 'axios';
    export default {
        name: "Login",
        data() {
            return {
                //1.提交表单
                user: {
                    email: '2578831517@qq.com',
                    password: '123456',
                },
               
                //3.限制规则
                rules: {
                    email: [{required: true, message: '请输入邮箱', trigger: 'blur'}],
                    password: [{required: true, message: '请输入密码', trigger: 'blur'}],
                },
                //4.防止前端重复提交
                logining: false,
            }
        },
        //加载立刻执行
        created(){

        },
        methods: {
           
            //提交表单
            submitForm(formName) {
             
                this.$refs[formName].validate(valid => {
                    if (valid) {

                        axios.post('/user/login',this.user).then(res=>{
                          if(res.status===300){
                            this.$message.error(res.data);
                          }
                          let token = res
                          localStorage.setItem('token',token);
                         
                            this.$router.push({
                              name:'index',
                              params:{
                                from:'login'
                              }
                           
                          })
                          
                        })

                    } else {
                        this.$message.error('用户名/密码');
                        this.logining = false;
                    }
                })
            }
        }
    }
</script>

<style scoped>
    .login-wrap {
        box-sizing: border-box;
        width: 100%;
        height: 100%;
        padding-top: 10%;
        background-image: url();
        background-repeat: no-repeat;
        background-position: center right;
        background-size: 100%;
    }

    .login-container {
        border-radius: 10px;
        margin: 0px auto;
        width: 350px;
        padding: 30px 35px 15px 35px;
        background: #fff;
        border: 1px solid #eaeaea;
        text-align: left;
        box-shadow: 0 0 20px 2px rgba(0, 0, 0, 0.1);
    }

    .title {
        margin: 0px auto 40px auto;
        text-align: center;
        color: #505458;
    }

    .remember {
        margin: 0px 0px 35px 0px;
    }

    .code-box {
        text-align: right;
    }

    .codeimg {
        height: 40px;
    }
</style>